package org.desktop.web.client.login.window;

import org.desktop.web.client.DesktopViewer;
import org.desktop.web.client.login.service.LoginService;
import org.desktop.web.client.login.service.LoginServiceAsync;
import org.desktop.web.client.util.Util;
import org.desktop.web.shared.Response;
import org.desktop.web.shared.constant.Token;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.PasswordField;
import com.sencha.gxt.widget.core.client.info.Info;

public class ChangePasswordWidget extends Dialog {

	private DesktopViewer desktop;
	private Token token;
	private LoginServiceAsync loginService = GWT.create(LoginService.class);
	
	
	PasswordField oldPasswordField;
	PasswordField newPasswordField;
	PasswordField repeatNewPasswordField;// visualizzabile solo per il cambio
											// password

	ContentPanel mainPanel;

	public ChangePasswordWidget(DesktopViewer desktopViewer,final Token token) {
		this.desktop = desktopViewer;
		this.token = token;

		this.mainPanel = new ContentPanel();
		this.mainPanel.setHeaderVisible(false);

		VerticalLayoutContainer vlc = new VerticalLayoutContainer();
		vlc.setPosition(0, 20);

		this.oldPasswordField = new PasswordField();
		this.oldPasswordField.setAllowBlank(false);
		this.oldPasswordField.setId("oldPassword");
		this.oldPasswordField.setName("oldPassword");

		this.newPasswordField = new PasswordField();
		this.newPasswordField.setAllowBlank(false);
		this.newPasswordField.setId("newPassword");
		this.newPasswordField.setName("newPassword");

		this.repeatNewPasswordField = new PasswordField();
		this.repeatNewPasswordField.setAllowBlank(false);
		this.repeatNewPasswordField.setId("newPassword");
		this.repeatNewPasswordField.setName("newPassword");

		VerticalLayoutData layoutData = new VerticalLayoutData();
		this.setHeaderVisible(true);
		switch (token) {
		case CHANGE:
			this.setHeadingText("Cambio password");
			vlc.add(new FieldLabel(this.oldPasswordField, "Vecchia Password") {
				{
					setLabelWordWrap(false);
					setLabelPad(30);
				}
			}, new VerticalLayoutData(1, -1, new Margins(0, 5, 10, 5)));
			break;
		case RECOVER:
			this.setHeadingText("Inserisci nuova password");
			break;
		default:
			break;
		}
		vlc.add(new FieldLabel(this.newPasswordField, "Nuova Password") {
			{
				setLabelWordWrap(false);
				setLabelPad(30);
			}
		}, new VerticalLayoutData(1, -1, new Margins(0, 5, 10, 5)));
		vlc.add(new FieldLabel(this.repeatNewPasswordField,
				"Ripeti Nuova Password") {
			{
				setLabelWordWrap(false);
				setLabelPad(30);
			}
		}, new VerticalLayoutData(1, -1, new Margins(0, 5, 10, 5)));

		this.mainPanel.add(vlc);

		this.add(this.mainPanel);

		this.setWidth(480);
		this.setHeight(300);
		this.setClosable(false);
		this.setModal(true);
		this.setDraggable(false);
		this.setButtonAlign(BoxLayoutPack.CENTER);

		this.setPredefinedButtons(PredefinedButton.OK,PredefinedButton.CANCEL);
		this.getButton(PredefinedButton.CANCEL).setText("Annulla");
		final PasswordField tempRep = this.repeatNewPasswordField;
		final PasswordField tempNew = this.newPasswordField;
		
		this.getButton(PredefinedButton.OK).addSelectHandler(
				new SelectHandler() {

					@Override
					public void onSelect(SelectEvent event) {
						switch(token){
						case RECOVER:
						if(tempRep.getText().trim().equals("") || tempNew.getText().equals("")){
							new AlertMessageBox("Error", "Inserire una password"){
								{
									setModal(true);
								}
							}.show();
							return;
						}
						Util.showWaitCursor();
						loginService.changePassword(tempNew.getText(),tempRep.getText(),Window.Location.getParameter("tkn"),new AsyncCallback<Response>() {

							@Override
							public void onFailure(Throwable arg0) {
								// TODO Auto-generated method stub
								
							}

							@Override
							public void onSuccess(Response result) {
								if(result.isSuccess()){
									JSONValue p = JSONParser.parseStrict(result.getJsonMetadata());
									JSONObject obj = null;
									if ((obj=p.isObject()) != null) {
//										= p.isObject();
										if(obj.get("error")!=null){
											AlertMessageBox amb = new AlertMessageBox("Error", obj.get("error").isString().stringValue());
											amb.show();
											Util.showDefaultCursor();
											return;
										}
										String token = obj.get("token").isString()
												.stringValue();
										Cookies.setCookie("token", token);
										Cookies.setCookie("functions", obj.get("functions").isArray().toString());
										Util.showDefaultCursor();
										
										
//										Window.alert(DesktopViewer.token);
									}

									Window.Location.assign("http://hrpo:8888/DesktopViewer.html");
								}
								
							}
						});
						break;
					}
					}
				});
		this.show();
	}
}
